﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace eTMS.Controls
{

    public partial class ucCommandButton : UserControl
    {
        public ucCommandButton()
        {
            InitializeComponent();
        }
        public event EventHandler OnAddNew;
        public event EventHandler OnEdit;
        public event EventHandler OnDelete;
        public event EventHandler OnSkip;
        public event EventHandler OnSave;
        public event EventHandler OnPrint;
        public event EventHandler OnImport;
        public event EventHandler OnExport;
        public event EventHandler OnExit;
        private bool IsEditable = true;
        private bool IsAddnewable = true;
        private bool IsDeleteable = true;
        private int LastButtonX;
        public void AddButton(eTMS.Controls.Button button)
        {
            button.Location = new Point(LastButtonX, btnAddNew.Location.Y);
            button.Height = btnAddNew.Height;
            this.Controls.Add(button);
            LastButtonX += button.Width + 5;
        }
        public void LockControl(bool IsLock)
        {
            this.btnAddNew.Enabled = IsLock && IsAddnewable;
            this.btnEdit.Enabled = IsLock && IsEditable;
            this.btnDel.Enabled = IsLock && IsDeleteable;
            this.btnSkip.Enabled = !IsLock;
            this.btnSave.Enabled = !IsLock;
            this.btnPrint.Enabled = IsLock;
        }
        public void SetFocusAddNew()
        {
            this.btnAddNew.Focus();
        }
        public void SetFocusSave()
        {
            this.btnSave.Focus();
        }
        public void SetFocusPrint()
        {
            this.btnPrint.Focus();
        }
        public void LockDel(bool IsLock)
        {
            this.btnDel.Enabled = IsLock;
        }
        public void LockEdit(bool IsLock)
        {
            this.btnEdit.Enabled = IsLock;
        }
        public void LockAdd(bool IsLock)
        {
            this.btnAddNew.Enabled = IsLock;
        }
        public void LockSave(bool IsLock)
        {
            this.btnSave.Enabled = IsLock;
        }
        public void SetEditPermission(bool enabled)
        {
            IsEditable = enabled;
            if (!IsEditable)
            {
                btnEdit.Enabled = false;
            }
        }
        public void SetDeletePermission(bool enabled)
        {
            IsDeleteable = enabled;
            if (!IsDeleteable)
            {
                btnDel.Enabled = false;
            }
        }
        public void SetAddnewPermission(bool enabled)
        {
            IsAddnewable = enabled;
            if (!IsAddnewable)
            {
                btnAddNew.Enabled = false;
            }
        }
        public eTMS.Controls.Button AddNewButton
        {
            get
            {
                return btnAddNew;
            }
        }
        public eTMS.Controls.Button SaveButton
        {
            get
            {
                return btnSave;
            }
        }

        public void SetSkipVisible(bool bVisible)
        {
            btnSkip.Visible = bVisible;

            mnuSkip.Visible = bVisible;
        }
        public void SetSkipTitle(string strTitle)
        {
            btnSkip.Text = strTitle;
        }
        public void SetSkipImage(Image image)
        {
            btnSkip.Image = image;
        }
        public void SetSkipWidth(int Width)
        {
            btnSkip.Width = Width;
        }

        public void SetSaveVisible(bool bVisible)
        {
            btnSave.Visible = bVisible;

            mnuSave.Visible = bVisible;
        }
        public void SetSaveTitle(string strTitle)
        {
            btnSave.Text = strTitle;
        }
        public void SetSaveWidth(int Width)
        {
            btnSave.Width = Width;
        }

        public void SetDeleteVisible(bool bVisible)
        {
            btnDel.Visible = bVisible;
            mnuDelete.Visible = bVisible;
        }
        public void SetDeleteTitle(string strTitle)
        {
            btnDel.Text = strTitle;
        }
        public void SetDeleteWidth(int Width)
        {
            btnDel.Width = Width;
        }

        public void SetPrintVisible(bool bVisible)
        {
            btnPrint.Visible = bVisible;

            mnuPrint.Visible = bVisible;
        }
        public void SetPrintTitle(string strTitle)
        {
            btnPrint.Text = strTitle;
        }
        public void SetPrintWidth(int Width)
        {
            btnPrint.Width = Width;
        }

        public void SetPrintImage(System.Drawing.Image image)
        {
            btnPrint.Image = image;
        }

        public void SetAddNewVisible(bool bVisible)
        {
            btnAddNew.Visible = bVisible;
            mnuAddNew.Visible = bVisible;

        }
        public void SetAddNewTitle(string strTitle)
        {
            btnAddNew.Text = strTitle;
        }
        public void SetAddNewWidth(int Width)
        {
            btnAddNew.Width = Width;
        }

        public void SetEditVisible(bool bVisible)
        {
            btnEdit.Visible = bVisible;
            mnuEdit.Visible = bVisible;
        }
        public void SetEditTitle(string strTitle)
        {
            btnEdit.Text = strTitle;
        }
        public void SetEditWidth(int Width)
        {
            btnEdit.Width = Width;
        }

        public void SetImportVisible(bool bVisible)
        {
            btnImport.Visible = bVisible;

        }
        public void SetImportTitle(string strTitle)
        {
            btnImport.Text = strTitle;
        }
        public void SetImportWidth(int Width)
        {
            btnImport.Width = Width;
        }

        public void SetExportVisible(bool bVisible)
        {
            btnExport.Visible = bVisible;
        }
        public void SetExportTitle(string strTitle)
        {
            btnExport.Text = strTitle;
        }
        public void SetExportWidth(int Width)
        {
            btnExport.Width = Width;
        }
        public void SetExportImage(Image image)
        {
            btnExport.Image = image;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (OnExit != null)
            {
                OnExit(sender, e);
            }
            if (this.ParentForm != null)
                this.ParentForm.Close();
        }

        private void btnSkip_Click(object sender, EventArgs e)
        {
            if (OnSkip != null)
            {
                OnSkip(sender, e);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (OnSave != null)
            {
                OnSave(sender, e);
            }
        }
        public void AddSortKey(Keys keys, string MenuText, Button button)
        {
            ToolStripMenuItem item = new ToolStripMenuItem();
            contextMenuStrip1.Items.Insert(0, item);
            item.Text = MenuText;
            item.ShortcutKeys = keys;
            item.Click += new EventHandler(item_Click);
            item.Tag = button;
        }

        void item_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            Button button = (Button)item.Tag;
            if (button.Enabled)
                button.PerformClick();

        }
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (OnDelete != null)
            {
                OnDelete(sender, e);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (OnEdit != null)
            {
                OnEdit(sender, e);
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if (OnAddNew != null)
            {
                OnAddNew(sender, e);
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!this.DesignMode)
            {
                this.BackColor = Color.FromArgb(255, 255, 255);
                this.StartGradientColor = Color.FromArgb(215, 229, 247);
                this.EndGradientColor = Color.FromArgb(186, 211, 245);
                //this.BorderColor = Color.RoyalBlue;
                ResetAllPos();
                if (this.ParentForm != null)
                {
                    this.ParentForm.CancelButton = btnSkip;
                    this.ParentForm.ContextMenuStrip = this.contextMenuStrip1;
                    this.contextMenuStrip1.Visible = false;
                }
            }
        }
        public void ResetAllPos()
        {
            int x = btnAddNew.Location.X;
            if (btnAddNew.Visible)
            {
                x += btnAddNew.Width + 5;
            }
            btnEdit.Location = new Point(x, btnEdit.Location.Y);
            if (btnEdit.Visible)
            {
                x += btnEdit.Width + 5;
            }
            btnDel.Location = new Point(x, btnDel.Location.Y);
            if (btnDel.Visible)
            {
                x += btnDel.Width + 5;
            }
            btnSave.Location = new Point(x, btnSave.Location.Y);
            if (btnSave.Visible)
            {
                x += btnSkip.Width + 5;
            }
            btnSkip.Location = new Point(x, btnSkip.Location.Y);
            if (btnSkip.Visible)
            {
                x += btnSkip.Width + 5;
            }
            btnPrint.Location = new Point(x, btnPrint.Location.Y);
            if (btnPrint.Visible)
            {
                x += btnPrint.Width + 5;
            }
            btnImport.Location = new Point(x, btnImport.Location.Y);
            if (btnImport.Visible)
            {
                x += btnImport.Width + 5;
            }
            btnExport.Location = new Point(x, btnExport.Location.Y);
            if (btnExport.Visible)
            {
                x += btnExport.Width + 5;
            }
            LastButtonX = x;
            this.ParentForm.CancelButton = btnExit;
            btnAddNew.DialogResult = DialogResult.None;
            btnSkip.DialogResult = DialogResult.None;
            btnEdit.DialogResult = DialogResult.None;
            btnExport.DialogResult = DialogResult.None;
            btnImport.DialogResult = DialogResult.None;
            btnSave.DialogResult = DialogResult.None;
            btnPrint.DialogResult = DialogResult.None;
        }
        public Button ExportButton
        {
            get
            {
                return btnExport;
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawBackGround(e.Graphics);

        }
        private Color endgradColor = Color.FromArgb(220, 220, 230);
        private Color startgradColor = Color.FromArgb(220, 230, 240);
        private LinearGradientMode gradientMode = LinearGradientMode.Vertical;
        private float gradPercent = 0.5f;
        public LinearGradientMode GradientMode
        {
            get
            {
                return gradientMode;
            }
            set
            {
                gradientMode = value;
                this.Invalidate();
            }
        }
        public float GradientPercent
        {
            get
            {
                return gradPercent;
            }
            set
            {
                gradPercent = value;
                this.Invalidate();
            }
        }
        public Color StartGradientColor
        {
            get
            {
                return startgradColor;
            }
            set
            {
                startgradColor = value;
                this.Invalidate();
            }
        }
        public Color EndGradientColor
        {
            get
            {
                return endgradColor;
            }
            set
            {
                endgradColor = value;
                this.Invalidate();
            }
        }

        private void DrawBackGround(Graphics g)
        {
            // Create a rectangle
            g.Clear(this.BackColor);
            Rectangle rect = this.ClientRectangle;
            // Create a linear gradient brush
            LinearGradientBrush rgBrush =
                new LinearGradientBrush(
                rect, this.startgradColor, this.endgradColor, gradientMode
            );					// Set signma bell shape
            rgBrush.SetSigmaBellShape(gradPercent, 1.0f);
            // Fill rectangle again
            g.FillRectangle(rgBrush, rect);
            // Dispose of object
            Blend blend = new Blend();
            float[] factArray = { 0.0f, 0.3f, 0.8f, 1.0f };
            float[] posArray = { 0.0f, 0.4f, 0.6f, 1.0f };
            // Set Factors and Positions properties of Blend
            blend.Factors = factArray;
            blend.Positions = posArray;
            // Set smoothing mode of Graphics object
            g.SmoothingMode = SmoothingMode.AntiAlias;
            // Create path and add a rectangle
            GraphicsPath path = new GraphicsPath();
            Rectangle rc = new Rectangle(rect.X, rect.Y - rect.Height, rect.Width, 5 * rect.Height);
            path.AddEllipse(rc);
            // Create path gradient brush
            PathGradientBrush pthBrush =
                new PathGradientBrush(path);
            // Set Blend and FocusScales properties
            pthBrush.Blend = blend;
            pthBrush.FocusScales = new PointF(0.7f, 0.2f);
            Color[] colors = { Color.FromArgb(10, this.startgradColor) };
            // Set CenterColor and SurroundColors properties
            pthBrush.CenterColor = Color.FromArgb(150, Color.White);
            pthBrush.SurroundColors = colors;
            g.FillEllipse(pthBrush, rc);
            pthBrush.Dispose();

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (OnPrint != null)
            {
                OnPrint(sender, e);
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (OnImport != null)
            {
                OnImport(sender, e);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (OnExport != null)
            {
                OnExport(sender, e);
            }
        }

        private void ActiveCmd(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.F2:
                    if ((btnAddNew.Enabled) && (btnAddNew.Visible))
                        btnAddNew.PerformClick();
                    break;
                case Keys.F3:
                    if ((btnEdit.Enabled) && (btnEdit.Visible))
                        btnEdit.PerformClick();
                    break;
                case Keys.F4:
                    if ((btnDel.Enabled) && (btnDel.Visible))
                        btnDel.PerformClick();
                    break;
                case Keys.F5:
                    if ((btnSkip.Enabled) && (btnSkip.Visible))
                        btnSkip.PerformClick();
                    break;
                case Keys.F10:
                    if ((btnSave.Enabled) && (btnSave.Visible))
                        btnSave.PerformClick();
                    break;
                case Keys.Escape:
                    if ((btnExit.Enabled) && (btnExit.Visible))
                        btnExit.PerformClick();
                    break;
            }
        }

        private void mnuAddNew_Click(object sender, EventArgs e)
        {
            RaiseAddNew();
        }

        private void mnuEdit_Click(object sender, EventArgs e)
        {
            RaiseEdit();
        }

        private void mnuSave_Click(object sender, EventArgs e)
        {
            RaiseSave();
        }

        private void mnuPrint_Click(object sender, EventArgs e)
        {
            RaisePrint();
        }

        private void mnuDelete_Click(object sender, EventArgs e)
        {
            RaiseDelete();
        }

        private void mnuSkip_Click(object sender, EventArgs e)
        {
            RaiseSkip();
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            RaiseExit();
        }



        public void RaiseAddNew()
        {
            if (btnAddNew.Enabled && btnAddNew.Visible)
            {
                btnAddNew.PerformClick();
            }
        }
        public void RaiseEdit()
        {
            if (btnEdit.Enabled && btnEdit.Visible)
            {
                btnEdit.PerformClick();
            }
        }
        public void RaiseDelete()
        {
            if (btnDel.Enabled && btnDel.Visible)
            {
                btnDel.PerformClick();
            }
        }
        public void RaiseSkip()
        {
            if (btnSkip.Enabled && btnSkip.Visible)
            {
                btnSkip.PerformClick();
            }
        }
        public void RaiseSave()
        {
            if (btnSave.Enabled && btnSave.Visible)
            {
                btnSave.PerformClick();
            }
        }
        public void RaisePrint()
        {
            if (btnPrint.Enabled && btnPrint.Visible)
            {
                btnPrint.PerformClick();
            }
        }
        public void RaiseExit()
        {
            if (btnExit.Enabled && btnExit.Visible)
            {
                btnExit.PerformClick();
            }
        }
    }


}